/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.data.gwt;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.data.core.DataManager;
import com.sinai.mshab.data.core.DataManagerCollection;

/**
 * A concrete implementation of {@link DataManagerCollection} that can return
 * {@link DataManager}s for GWT based widgets.
 * 
 * @author Arthur Kalmenson
 */
public class GwtDataManagerCollection implements DataManagerCollection {

	/**
	 * A map of GWT class names to {@link DataManager}s.
	 */
	private Map<String, DataManager<?, ?>> classnameToManager;

	/**
	 * Creates a new {@code GwtDataManagerCollection} and maps classnames to
	 * {@link DataManager}s.
	 */
	public GwtDataManagerCollection() {
		classnameToManager = new HashMap<String, DataManager<?, ?>>();

		// populate our classnameToManager with GWT widget managers.

		// for TextBoxBase widgets.
		DataManager<TextBoxBase, String> textBoxBaseDataManager =
				new TextBoxBaseDataManager();
		classnameToManager.put(TextBox.class.getName(), textBoxBaseDataManager);
		classnameToManager
				.put(TextArea.class.getName(), textBoxBaseDataManager);
		classnameToManager.put(PasswordTextBox.class.getName(),
				textBoxBaseDataManager);

		// for ListBox widgets.
		classnameToManager.put(ListBox.class.getName(),
				new ListBoxDataManager());
	}

	public DataManager<?, ?> getDataManager(Widget widget) {
		return classnameToManager.get(widget.getClass().getName());
	}

	public boolean hasDataManager(Widget widget) {
		return classnameToManager.containsKey(widget.getClass().getName());
	}

}
